home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / linux / tools / gtar10.lha / create.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-09  |  30.6 KB  |  1,316 lines

  1. /* Create a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Create a tar archive.
  22.  *
  23.  * Written 25 Aug 1985 by John Gilmore, ihnp4!hoptoad!gnu.
  24.  *
  25.  * @(#)create.c 1.36 11/6/87 - gnu
  26.  */
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <stdio.h>
  30.  
  31. #ifndef V7
  32. #include <fcntl.h>
  33. #endif
  34.  
  35. #ifndef    __MSDOS__
  36. #include <sys/file.h>
  37. #include <sys/param.h>        /* for MAXPATHLEN */
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #endif
  41.  
  42. #ifdef BSD42
  43. #include <sys/dir.h>
  44. #else
  45. #ifdef __MSDOS__
  46. #include "msd_dir.h"
  47. #else
  48. #ifdef USG
  49. #ifdef NDIR
  50. #include <ndir.h>
  51. #else
  52. #include <dirent.h>
  53. #endif
  54. #ifndef DIRECT
  55. #define direct dirent
  56. #endif
  57. #define DP_NAMELEN(x) strlen((x)->d_name)
  58. #else
  59. /*
  60.  * FIXME: On other systems there is no standard place for the header file
  61.  * for the portable directory access routines.  Change the #include line
  62.  * below to bring it in from wherever it is.
  63.  */
  64. #include "ndir.h"
  65. #endif
  66. #endif
  67. #endif
  68.  
  69. #ifndef DP_NAMELEN
  70. #define DP_NAMELEN(x)    (x)->d_namlen
  71. #endif
  72.  
  73. #ifdef USG
  74. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  75. #endif
  76.  
  77. /*
  78.  * V7 doesn't have a #define for this.
  79.  */
  80. #ifndef O_RDONLY
  81. #define    O_RDONLY    0
  82. #endif
  83.  
  84. /*
  85.  * Most people don't have a #define for this.
  86.  */
  87. #ifndef    O_BINARY
  88. #define    O_BINARY    0
  89. #endif
  90.  
  91. #ifndef MAXPATHLEN
  92. #define MAXPATHLEN 1024
  93. #endif
  94.  
  95. #include "tar.h"
  96. #include "port.h"
  97.  
  98. extern struct stat hstat;        /* Stat struct corresponding */
  99.  
  100. #ifndef __MSDOS__
  101. extern dev_t ar_dev;
  102. extern ino_t ar_ino;
  103. #endif
  104.  
  105. /* JF */
  106. extern struct name *gnu_list_name;
  107.  
  108. /*
  109.  * If there are no symbolic links, there is no lstat().  Use stat().
  110.  */
  111. #ifndef S_IFLNK
  112. #define lstat stat
  113. #endif
  114.  
  115. #ifndef __STDC__
  116. extern char    *malloc();
  117. extern char    *strcpy();
  118. extern char    *strncpy();
  119. extern void    bzero();
  120. extern void    bcopy();
  121. #endif
  122. extern int    errno;
  123.  
  124. extern void print_header();
  125.  
  126. union record *start_header();
  127. void finish_header();
  128. void finduname();
  129. void findgname();
  130. char *name_next();
  131. void to_oct();
  132. void dump_file();
  133.  
  134.  
  135. /* This code moved from tar.h since create.c is the only file that cares
  136.    about 'struct link's.  This means that other files might not have to
  137.    include sys/types.h any more.
  138.  */
  139.  
  140. struct link {
  141.     struct link    *next;
  142.     dev_t        dev;
  143.     ino_t        ino;
  144.     short        linkcount;
  145.     char        name[1];
  146. };
  147.  
  148. struct link    *linklist;    /* Points to first link in list */
  149.  
  150. static nolinks;            /* Gets set if we run out of RAM */
  151.  
  152. /*
  153.  * "Scratch" space to store the information about a sparse file before
  154.  * writing the info into the header or extended header
  155.  */
  156. /* struct sp_array     *sparsearray;*/
  157.  
  158. /* number of elts storable in the sparsearray */
  159. /*int     sparse_array_size = 10;*/
  160.  
  161. void
  162. create_archive()
  163. {
  164.     register char    *p;
  165.     char *name_from_list();
  166.  
  167.     open_archive(0);        /* Open for writing */
  168.  
  169.     if(f_gnudump) {
  170.         char buf[MAXNAMLEN],*q,*bufp;
  171.  
  172.         collect_and_sort_names();
  173.  
  174.         while(p=name_from_list())
  175.             dump_file(p,-1);
  176.         /* if(!f_dironly) { */
  177.             blank_name_list();
  178.             while(p=name_from_list()) {
  179.                 strcpy(buf,p);
  180.                 if(p[strlen(p)-1]!='/')
  181.                     strcat(buf,"/");
  182.                 bufp=buf+strlen(buf);
  183.                 for(q=gnu_list_name->dir_contents;q && *q;q+=strlen(q)+1) {
  184.                     if(*q=='Y') {
  185.                         strcpy(bufp,q+1);
  186.                         dump_file(buf,-1);
  187.                     }
  188.                 }
  189.             }
  190.         /* } */
  191.     
  192.     } else {
  193.         p = name_next(1);
  194.         if(!p)
  195.             dump_file(".", -1);
  196.         else {
  197.             do dump_file(p, -1);
  198.             while (p = name_next(1));
  199.         }
  200.     }
  201.  
  202.     write_mangled();
  203.     write_eot();
  204.     close_archive();
  205.     if(f_gnudump)
  206.         write_dir_file();
  207.     name_close();
  208. }
  209.  
  210. /*
  211.  * Dump a single file.  If it's a directory, recurse.
  212.  * Result is 1 for success, 0 for failure.
  213.  * Sets global "hstat" to stat() output for this file.
  214.  */
  215. void
  216. dump_file (p, curdev)
  217.     char    *p;            /* File name to dump */
  218.     int    curdev;            /* Device our parent dir was on */
  219. {
  220.     union record    *header;
  221.     char type;
  222.     extern char *save_name;        /* JF for multi-volume support */
  223.     extern long save_totsize;
  224.     extern long save_sizeleft;
  225.     union record    *exhdr;
  226.     char save_linkflag;
  227.     extern time_t new_time;
  228.     int sparse_ind = 0;
  229.  
  230.  
  231.     if(f_confirm && !confirm("add",p))
  232.         return;
  233.  
  234.     /*
  235.      * Use stat if following (rather than dumping) 4.2BSD's
  236.      * symbolic links.  Otherwise, use lstat (which, on non-4.2
  237.      * systems, is #define'd to stat anyway.
  238.      */
  239. #ifdef AIX
  240.     if (0 != f_follow_links ?
  241.         statx (p, &hstat, STATSIZE, STX_HIDDEN):
  242.         statx (p, &hstat, STATSIZE, STX_HIDDEN|STX_LINK))
  243. #else
  244.     if (0 != f_follow_links? stat(p, &hstat): lstat(p, &hstat))
  245. #endif /* AIX */
  246.     {
  247. badperror:
  248.         msg_perror("can't add file %s",p);
  249. badfile:
  250.         errors++;
  251.         return;
  252.     }
  253.  
  254. #ifdef AIX
  255.     if (S_ISHIDDEN (hstat.st_mode)) {
  256.         char *new = (char *)allocate (strlen (p) + 2);
  257.         if (new) {
  258.             strcpy (new, p);
  259.             strcat (new, "@");
  260.             p = new;
  261.         }
  262.     }
  263. #endif /* AIX */
  264.  
  265.     /* See if we only want new files, and check if this one is too old to
  266.        put in the archive. */
  267.     if(   f_new_files
  268.        && !f_gnudump
  269.         && new_time>hstat.st_mtime
  270.         && (hstat.st_mode&S_IFMT)!=S_IFDIR
  271.         && (f_new_files>1 || new_time>hstat.st_ctime)) {
  272.         if(curdev<0) {
  273.             msg("%s: is unchanged; not dumped",p);
  274.         }
  275.         return;
  276.     }
  277.  
  278. #ifndef __MSDOS__
  279.     /* See if we are trying to dump the archive */
  280.     if(ar_dev && hstat.st_dev==ar_dev && hstat.st_ino==ar_ino) {
  281.         msg("%s is the archive; not dumped",p);
  282.         return;
  283.     }
  284. #endif
  285.     /*
  286.      * Check for multiple links.
  287.      *
  288.      * We maintain a list of all such files that we've written so
  289.      * far.  Any time we see another, we check the list and
  290.      * avoid dumping the data again if we've done it once already.
  291.      */
  292.     if (hstat.st_nlink > 1) switch (hstat.st_mode & S_IFMT) {
  293.         register struct link    *lp;
  294.  
  295.     case S_IFREG:            /* Regular file */
  296. #ifdef S_IFCTG
  297.     case S_IFCTG:            /* Contigous file */
  298. #endif
  299. #ifdef S_IFCHR
  300.     case S_IFCHR:            /* Character special file */
  301. #endif
  302.  
  303. #ifdef S_IFBLK
  304.     case S_IFBLK:            /* Block     special file */
  305. #endif
  306.  
  307. #ifdef S_IFIFO
  308.     case S_IFIFO:            /* Fifo      special file */
  309. #endif
  310.  
  311.         /* First quick and dirty.  Hashing, etc later FIXME */
  312.         for (lp = linklist; lp; lp = lp->next) {
  313.             if (lp->ino == hstat.st_ino &&
  314.                 lp->dev == hstat.st_dev) {
  315.                 char *link_name = lp->name;
  316.  
  317.                 /* We found a link. */
  318.                 hstat.st_size = 0;
  319.                 header = start_header(p, &hstat);
  320.                 if (header == NULL) goto badfile;
  321.                 while(!f_absolute_paths && *link_name == '/') {
  322.                     static int link_warn = 0;
  323.  
  324.                     if (!link_warn) {
  325.                         msg("Removing leading / from absolute links");
  326.                         link_warn++;
  327.                     }
  328.                     link_name++;
  329.                 }
  330.                   strncpy(header->header.linkname,
  331.                     link_name,NAMSIZ);
  332.                 if(header->header.linkname[NAMSIZ-1]) {
  333.                     char *mangled;
  334.                     extern char *find_mangled();
  335.  
  336.                     mangled=find_mangled(link_name);
  337.                     msg("%s: link name too long: mangled to %s",link_name,mangled);
  338.                     strncpy(header->header.linkname,mangled,NAMSIZ);
  339.                 }
  340.                 header->header.linkflag = LF_LINK;
  341.                 finish_header(header);
  342.         /* FIXME: Maybe remove from list after all links found? */
  343.                 return;        /* We dumped it */
  344.             }
  345.         }
  346.  
  347.         /* Not found.  Add it to the list of possible links. */
  348.         lp = (struct link *)malloc((unsigned)(sizeof(struct link)+strlen(p)));
  349.         if (!lp) {
  350.             if (!nolinks) {
  351.                 msg(
  352.     "no memory for links, they will be dumped as separate files");
  353.                 nolinks++;
  354.             }
  355.         }
  356.         lp->ino = hstat.st_ino;
  357.         lp->dev = hstat.st_dev;
  358.         strcpy(lp->name, p);
  359.         lp->next = linklist;
  360.         linklist = lp;
  361.     }
  362.  
  363.     /*
  364.      * This is not a link to a previously dumped file, so dump it.
  365.      */
  366.     switch (hstat.st_mode & S_IFMT) {
  367.  
  368.     case S_IFREG:            /* Regular file */
  369. #ifdef S_IFCTG
  370.     case S_IFCTG:            /* Contiguous file */
  371. #endif
  372.     {
  373.         int    f;        /* File descriptor */
  374.         long    bufsize, count;
  375.         long    sizeleft;
  376.         register union record     *start;
  377.         int     header_moved;
  378.         char    isextended = 0;
  379.         int     upperbound;
  380.         int    end_nulls = 0;
  381.         
  382.         header_moved = 0;
  383.  
  384. #ifdef BSD42
  385.         if (f_sparse_files) {
  386.         /*
  387.           * JK - This is the test for sparseness: whether the
  388.          * "size" of the file matches the number of blocks
  389.          * allocated for it.  If there is a smaller number
  390.          * of blocks that would be necessary to accommodate
  391.          * a file of this size, we have a sparse file, i.e.,
  392.          * at least one of those records in the file is just
  393.          * a useless hole.
  394.          */
  395. #ifdef hpux    /* Nice of HPUX to gratuitiously change it, huh?  - mib */
  396.                 if (hstat.st_size - (hstat.st_blocks * 1024) > 1024 ) {
  397. #else
  398.             if (hstat.st_size - (hstat.st_blocks * RECORDSIZE) > RECORDSIZE) {
  399. #endif
  400.                 int    filesize = hstat.st_size;
  401.                 register int i;
  402.                 
  403.                 header = start_header(p, &hstat);
  404.                 if (header == NULL)
  405.                     goto badfile;
  406.                 header->header.linkflag = LF_SPARSE;
  407.                 header_moved++;
  408.                 
  409.             /*
  410.              * Call the routine that figures out the
  411.              * layout of the sparse file in question.
  412.              * UPPERBOUND is the index of the last
  413.              * element of the "sparsearray," i.e.,
  414.              * the number of elements it needed to
  415.              * describe the file.
  416.              */
  417.                  
  418.                 upperbound = deal_with_sparse(p, header);
  419.                          
  420.             /* 
  421.              * See if we'll need an extended header
  422.              * later
  423.              */
  424.                 if (upperbound > SPARSE_IN_HDR-1)
  425.                      header->header.isextended++;
  426.             /*
  427.              * We store the "real" file size so
  428.              * we can show that in case someone wants
  429.              * to list the archive, i.e., tar tvf <file>.
  430.              * It might be kind of disconcerting if the
  431.              * shrunken file size was the one that showed
  432.              * up.
  433.              */
  434.                  to_oct((long) hstat.st_size, 1+12, 
  435.                          header->header.realsize);
  436.                     
  437.             /*
  438.              * This will be the new "size" of the
  439.              * file, i.e., the size of the file
  440.              * minus the records of holes that we're
  441.              * skipping over. 
  442.              */
  443.                  
  444.                 find_new_file_size(&filesize, upperbound);
  445.                 hstat.st_size = filesize;
  446.                 to_oct((long) filesize, 1+12,
  447.                          header->header.size);
  448. /*                to_oct((long) end_nulls, 1+12, 
  449.                         header->header.ending_blanks);*/
  450.                         
  451.                 for (i = 0; i < SPARSE_IN_HDR; i++) {
  452.                     if (!sparsearray[i].numbytes)
  453.                         break;
  454.                     to_oct(sparsearray[i].offset, 1+12,
  455.                         header->header.sp[i].offset);
  456.                     to_oct(sparsearray[i].numbytes, 1+12,
  457.                         header->header.sp[i].numbytes);
  458.                 }
  459.                     
  460.             }
  461.         }
  462. #else
  463.         upperbound=SPARSE_IN_HDR-1;
  464. #endif
  465.         
  466.         sizeleft = hstat.st_size;
  467.         /* Don't bother opening empty, world readable files. */
  468.         if (sizeleft > 0 || 0444 != (0444 & hstat.st_mode)) {
  469.             f = open(p, O_RDONLY|O_BINARY);
  470.             if (f < 0) goto badperror;
  471.         } else {
  472.             f = -1;
  473.         }
  474.         
  475.         /* If the file is sparse, we've already taken care of this */
  476.         if (!header_moved) {
  477.             header = start_header(p, &hstat);
  478.             if (header == NULL) {
  479.                 if(f>=0)
  480.                     (void)close(f);
  481.                 goto badfile;
  482.             }
  483.         }
  484. #ifdef S_IFCTG
  485.         /* Mark contiguous files, if we support them */
  486.         if (f_standard && (hstat.st_mode & S_IFMT) == S_IFCTG) {
  487.             header->header.linkflag = LF_CONTIG;
  488.         }
  489. #endif
  490.         isextended = header->header.isextended;
  491.         save_linkflag = header->header.linkflag;
  492.         finish_header(header);
  493.         if (isextended) {
  494.             int     sum = 0;
  495.             register int i;
  496. /*            register union record *exhdr;*/
  497.             int     arraybound = SPARSE_EXT_HDR;
  498.             /* static */ int index_offset = SPARSE_IN_HDR;
  499.             
  500.     extend:        exhdr = findrec();
  501.             
  502.             if (exhdr == NULL) goto badfile;
  503.             bzero(exhdr->charptr, RECORDSIZE);
  504.             for (i = 0; i < SPARSE_EXT_HDR; i++) {
  505.                 if (i+index_offset > upperbound)
  506.                     break;
  507.                 to_oct((long) sparsearray[i+index_offset].numbytes,
  508.                      1+12,
  509.                     exhdr->ext_hdr.sp[i].numbytes);
  510.                 to_oct((long) sparsearray[i+index_offset].offset,
  511.                      1+12,
  512.                     exhdr->ext_hdr.sp[i].offset);
  513.             }
  514.             userec(exhdr);
  515. /*            sum += i;
  516.             if (sum < upperbound)
  517.                 goto extend;*/
  518.             if (index_offset+i < upperbound) {
  519.                 index_offset += i;
  520.                 exhdr->ext_hdr.isextended++;
  521.                 goto extend;
  522.             }
  523.                 
  524.         }
  525.         if (save_linkflag == LF_SPARSE) {
  526.             if (finish_sparse_file(f, &sizeleft, hstat.st_size, p))
  527.                 goto padit;
  528.         }
  529.         else
  530.           while (sizeleft > 0) {
  531.             
  532.             if(f_multivol) {   
  533.                 save_name = p;
  534.                 save_sizeleft = sizeleft;
  535.                 save_totsize = hstat.st_size;
  536.             }
  537.             start = findrec();
  538.  
  539.             bufsize = endofrecs()->charptr - start->charptr;
  540.                 
  541.             if (sizeleft < bufsize) {
  542.                 /* Last read -- zero out area beyond */
  543.                 bufsize = (int)sizeleft;
  544.                 count = bufsize % RECORDSIZE;
  545.                 if (count) 
  546.                     bzero(start->charptr + sizeleft,
  547.                         (int)(RECORDSIZE - count));
  548.             }
  549.             count = read(f, start->charptr, bufsize);
  550.             if (count < 0) {
  551.                 msg_perror("read error at byte %ld, reading\
  552.  %d bytes, in file %s",  hstat.st_size - sizeleft, bufsize,p);
  553.                 goto padit;
  554.             }
  555.             sizeleft -= count;
  556.  
  557.             /* This is nonportable (the type of userec's arg). */
  558.             userec(start+(count-1)/RECORDSIZE);
  559.  
  560.             if (count == bufsize) continue;
  561.             msg( "file %s shrunk by %d bytes, padding with zeros.", p, sizeleft);
  562.             goto padit;        /* Short read */
  563.         }
  564.  
  565.         if(f_multivol)
  566.             save_name = 0;
  567.  
  568.         if (f >= 0)
  569.             (void)close(f);
  570.  
  571.         break;
  572.  
  573.         /*
  574.          * File shrunk or gave error, pad out tape to match
  575.          * the size we specified in the header.
  576.          */
  577.     padit:
  578.         while(sizeleft>0) {
  579.             save_sizeleft=sizeleft;
  580.             start=findrec();
  581.             bzero(start->charptr,RECORDSIZE);
  582.             userec(start);
  583.             sizeleft-=RECORDSIZE;
  584.         }
  585.         if(f_multivol)
  586.             save_name=0;
  587.         if(f>=0)
  588.             (void)close(f);
  589.         break;
  590. /*        abort(); */
  591.     }
  592.  
  593. #ifdef S_IFLNK
  594.     case S_IFLNK:            /* Symbolic link */
  595.     {
  596.         int size;
  597.  
  598.         hstat.st_size = 0;        /* Force 0 size on symlink */
  599.         header = start_header(p, &hstat);
  600.         if (header == NULL) goto badfile;
  601.         size = readlink(p, header->header.linkname, NAMSIZ);
  602.         if (size < 0) goto badperror;
  603.         if (size == NAMSIZ) {
  604.             char buf[MAXPATHLEN];
  605.  
  606.             readlink(p,buf,MAXPATHLEN);
  607.             /* next_mangle(header->header.linkname); */
  608.             add_symlink_mangle(buf,p,header->header.linkname);
  609.             msg("symbolic link %s too long: mangling to %s",p, header->header.linkname);
  610.             /* size=strlen(header->header.linkname); */
  611.         } else
  612.             header->header.linkname[size] = '\0';
  613.         header->header.linkflag = LF_SYMLINK;
  614.         finish_header(header);        /* Nothing more to do to it */
  615.     }
  616.         break;
  617. #endif
  618.  
  619.     case S_IFDIR:            /* Directory */
  620.     {
  621.         register DIR *dirp;
  622.         register struct direct *d;
  623.         char *namebuf;
  624.         int buflen;
  625.         register int len;
  626.         int our_device = hstat.st_dev;
  627.         extern char *ck_malloc(),*ck_realloc();
  628.  
  629.         /* Build new prototype name */
  630.         len = strlen(p);
  631.         buflen=len+NAMSIZ;
  632.         namebuf=ck_malloc(buflen+1);
  633.         strncpy(namebuf, p, buflen);
  634.         while (len >= 1 && '/' == namebuf[len-1]) 
  635.             len--;            /* Delete trailing slashes */
  636.         namebuf[len++] = '/';        /* Now add exactly one back */
  637.         namebuf[len] = '\0';        /* Make sure null-terminated */
  638.  
  639.         /*
  640.          * Output directory header record with permissions
  641.          * FIXME, do this AFTER files, to avoid R/O dir problems?
  642.          * If old archive format, don't write record at all.
  643.          */
  644.         if (!f_oldarch) {
  645.             hstat.st_size = 0;    /* Force 0 size on dir */
  646.             /*
  647.              * If people could really read standard archives,
  648.              * this should be:        (FIXME)
  649.             header = start_header(f_standard? p: namebuf, &hstat);
  650.              * but since they'd interpret LF_DIR records as
  651.              * regular files, we'd better put the / on the name.
  652.              */
  653.             header = start_header(namebuf, &hstat);
  654.             if (header == NULL)
  655.                 goto badfile;    /* eg name too long */
  656.  
  657.             if (f_gnudump)
  658.                 header->header.linkflag = LF_DUMPDIR;
  659.             else if (f_standard)
  660.                 header->header.linkflag = LF_DIR;
  661.  
  662.             /* If we're gnudumping, we aren't done yet so don't close it. */
  663.             if(!f_gnudump)
  664.                 finish_header(header);    /* Done with directory header */
  665.         }
  666.  
  667.         if(f_gnudump) {
  668.             int sizeleft;
  669.             int totsize;
  670.             int bufsize;
  671.             union record *start;
  672.             int count;
  673.             char *buf,*p_buf;
  674.  
  675.             buf=gnu_list_name->dir_contents; /* FOO */
  676.             totsize=0;
  677.             for(p_buf=buf;p_buf && *p_buf;) {
  678.                 int tmp;
  679.  
  680.                 tmp=strlen(p_buf)+1;
  681.                 totsize+=tmp;
  682.                 p_buf+=tmp;
  683.             }
  684.             totsize++;
  685.             to_oct((long)totsize,1+12,header->header.size);
  686.             finish_header(header);
  687.             p_buf=buf;
  688.             sizeleft=totsize;
  689.             while(sizeleft>0) {
  690.                 if(f_multivol) {
  691.                     save_name=p;
  692.                     save_sizeleft=sizeleft;
  693.                     save_totsize=totsize;
  694.                 }
  695.                 start=findrec();
  696.                 bufsize=endofrecs()->charptr - start->charptr;
  697.                 if(sizeleft<bufsize) {
  698.                     bufsize=sizeleft;
  699.                     count=bufsize%RECORDSIZE;
  700.                     if(count)
  701.                         bzero(start->charptr+sizeleft,RECORDSIZE-count);
  702.                 }
  703.                 bcopy(p_buf,start->charptr,bufsize);
  704.                 sizeleft-=bufsize;
  705.                 p_buf+=bufsize;
  706.                 userec(start+(bufsize-1)/RECORDSIZE);
  707.             }
  708.             if(f_multivol)
  709.                 save_name = 0;
  710.              break;
  711.         }
  712.  
  713.         /* Now output all the files in the directory */
  714.         /* if (f_dironly)
  715.             break;        /* Unless the cmdline said not to */
  716.         /*
  717.          * See if we are crossing from one file system to another,
  718.          * and avoid doing so if the user only wants to dump one file system.
  719.          */
  720.         if (f_local_filesys && curdev >= 0 && curdev != hstat.st_dev) {
  721.             if(f_verbose)
  722.                 msg("%s: is on a different filesystem; not dumped",p);
  723.             break;
  724.         }
  725.  
  726.  
  727.         errno = 0;
  728.         dirp = opendir(p);
  729.         if (!dirp) {
  730.             if (errno) {
  731.                 msg_perror ("can't open directory %s",p);
  732.             } else {
  733.                 msg("error opening directory %s",
  734.                     p);
  735.             }
  736.             break;
  737.         }
  738.  
  739.         /* Hack to remove "./" from the front of all the file names */
  740.         if (len == 2 && namebuf[0] == '.' && namebuf[1]=='/')
  741.             len = 0;
  742.  
  743.         /* Should speed this up by cd-ing into the dir, FIXME */
  744.         while (NULL != (d=readdir(dirp))) {
  745.             /* Skip . and .. */
  746.             if(is_dot_or_dotdot(d->d_name))
  747.                 continue;
  748.  
  749.             if (DP_NAMELEN(d) + len >= buflen) {
  750.                 buflen=len+DP_NAMELEN(d);
  751.                 namebuf=ck_realloc(namebuf,buflen+1);
  752.                 /* namebuf[len]='\0';
  753.                 msg("file name %s%s too long", 
  754.                     namebuf, d->d_name);
  755.                 continue; */
  756.             }
  757.             strcpy(namebuf+len, d->d_name);
  758.             if(f_exclude && check_exclude(namebuf))
  759.                 continue;
  760.             dump_file(namebuf, our_device);
  761.         }
  762.  
  763.         closedir(dirp);
  764.         free(namebuf);
  765.     }
  766.         break;
  767.  
  768. #ifdef S_IFCHR
  769.     case S_IFCHR:            /* Character special file */
  770.         type = LF_CHR;
  771.         goto easy;
  772. #endif
  773.  
  774. #ifdef S_IFBLK
  775.     case S_IFBLK:            /* Block     special file */
  776.         type = LF_BLK;
  777.         goto easy;
  778. #endif
  779.  
  780. /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
  781. #if ((_ISP__M68K == 0) && (_ISP__A88K == 0))
  782. #ifdef S_IFIFO
  783.     case S_IFIFO:            /* Fifo      special file */
  784.         
  785.         type = LF_FIFO;
  786.         goto easy;
  787. #endif
  788. #endif
  789.  
  790. #ifdef S_IFSOCK
  791.     case S_IFSOCK:            /* Socket    pretend its a fifo? */
  792.         type = LF_FIFO;
  793.         goto easy;
  794. #endif
  795.  
  796.     easy:
  797.         if (!f_standard) goto unknown;
  798.  
  799.         hstat.st_size = 0;        /* Force 0 size */
  800.         header = start_header(p, &hstat);
  801.         if (header == NULL) goto badfile;    /* eg name too long */
  802.  
  803.         header->header.linkflag = type;
  804.         if (type != LF_FIFO) {
  805.             to_oct((long) major(hstat.st_rdev), 8,
  806.                 header->header.devmajor);
  807.             to_oct((long) minor(hstat.st_rdev), 8,
  808.                 header->header.devminor);
  809.         }
  810.  
  811.         finish_header(header);
  812.         break;
  813.  
  814.     default:
  815.     unknown:
  816.         msg("%s: Unknown file type; file ignored.", p);
  817.         break;
  818.     }
  819. }
  820.  
  821. int
  822. finish_sparse_file(fd, sizeleft, fullsize, name)
  823.     int    fd;
  824.     long     *sizeleft,
  825.         fullsize;
  826.     char    *name;
  827. {
  828.     union record    *start;
  829.     char        tempbuf[RECORDSIZE];
  830.     int        bufsize,
  831.             sparse_ind = 0,
  832.             count;
  833.     long        pos;
  834.     long        nwritten = 0;
  835.  
  836.  
  837.     while (*sizeleft > 0) {
  838.         start = findrec();
  839.         bzero(start->charptr, RECORDSIZE);
  840.         bufsize = sparsearray[sparse_ind].numbytes;
  841.         if (!bufsize) {  /* we blew it, maybe */
  842.                 msg("Wrote %ld of %ld bytes to file %s",
  843.                        fullsize - *sizeleft, fullsize, name);
  844.             break;
  845.              }
  846.         pos = lseek(fd, sparsearray[sparse_ind++].offset, 0);
  847.         /* 
  848.          * If the number of bytes to be written here exceeds
  849.          * the size of the temporary buffer, do it in steps.
  850.          */
  851.         while (bufsize > RECORDSIZE) {
  852. /*            if (amt_read) {
  853.                 count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
  854.                 bufsize -= RECORDSIZE - amt_read;
  855.                 amt_read = 0;
  856.                 userec(start);
  857.                 start = findrec();
  858.                 bzero(start->charptr, RECORDSIZE);
  859.             }*/
  860.             /* store the data */
  861.             count = read(fd, start->charptr, RECORDSIZE);
  862.             if (count < 0)     {
  863.                 msg_perror("read error at byte %ld, reading %d bytes, in file %s", 
  864.                         fullsize - *sizeleft, bufsize, name);
  865.                 return 1;
  866.             }            
  867.             bufsize -= count;
  868.             *sizeleft -= count;
  869.             userec(start);
  870.             nwritten += RECORDSIZE;    /* XXX */
  871.             start = findrec();
  872.             bzero(start->charptr, RECORDSIZE);
  873.         }
  874.  
  875.  
  876.         clear_buffer(tempbuf);
  877.         count = read(fd, tempbuf, bufsize);
  878.         bcopy(tempbuf, start->charptr, RECORDSIZE);
  879.         if (count < 0)     {
  880.             msg_perror("read error at byte %ld, reading %d bytes, in file %s", 
  881.                     fullsize - *sizeleft, bufsize, name);
  882.             return 1;
  883.         }
  884. /*        if (amt_read >= RECORDSIZE) {
  885.             amt_read = 0;
  886.             userec(start+(count-1)/RECORDSIZE);
  887.             if (count != bufsize) {
  888.                 msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
  889.                 return 1;
  890.             }
  891.             start = findrec();
  892.         } else 
  893.             amt_read += bufsize;*/
  894.         nwritten += count; /* XXX */
  895.         *sizeleft -= count;
  896.         userec(start);
  897.  
  898.     }
  899.     free(sparsearray);
  900.     printf ("Amount actually written is (I hope) %d.\n", nwritten);
  901. /*    userec(start+(count-1)/RECORDSIZE);*/
  902.     return 0;
  903.  
  904. }
  905.  
  906. init_sparsearray()
  907. {
  908.     register int i;
  909.  
  910.     sp_array_size = 10;
  911.     /* 
  912.      * Make room for our scratch space -- initially is 10 elts long
  913.      */
  914.     sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
  915.     for (i = 0; i < sp_array_size; i++) {
  916.         sparsearray[i].offset = 0;
  917.         sparsearray[i].numbytes = 0;
  918.     }
  919. }
  920.  
  921.  
  922.  
  923. /*
  924.  * Okay, we've got a sparse file on our hands -- now, what we need to do is
  925.  * make a pass through the file and carefully note where any data is, i.e.,
  926.  * we want to find how far into the file each instance of data is, and how
  927.  * many bytes are there.  We store this information in the sparsearray,
  928.  * which will later be translated into header information.  For now, we use
  929.  * the sparsearray as convenient storage.
  930.  *
  931.  * As a side note, this routine is a mess.  If I could have found a cleaner
  932.  * way to do it, I would have.  If anyone wants to find a nicer way to do
  933.  * this, feel free.
  934.  */
  935.  
  936. /* There is little point in trimming small amounts of null data at the */
  937.  /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
  938.  /* of complete null data */
  939. int
  940. deal_with_sparse(name, header, nulls_at_end)
  941.     char        *name;
  942.     union record     *header;
  943.     
  944. {
  945.     long    numbytes = 0;
  946.     long    offset = 0;
  947.     long    save_offset;
  948.     int    fd;
  949.     int    current_size = hstat.st_size;
  950.     int    sparse_ind = 0,
  951.         cc;
  952.     char    buf[RECORDSIZE];
  953.     int    read_last_data = 0; /* did we just read the last record? */
  954.     int     amidst_data = 0;
  955.     
  956.     header->header.isextended = 0;
  957.     /* 
  958.      * Can't open the file -- this problem will be caught later on,
  959.      * so just return.
  960.      */
  961.     if ((fd = open(name, O_RDONLY)) < 0)
  962.         return 0;
  963.         
  964.     init_sparsearray();
  965.     clear_buffer(buf);
  966.  
  967.     while ((cc = read(fd, buf, sizeof buf)) != 0) {
  968.             
  969.         if (sparse_ind > sp_array_size-1) {
  970.         
  971.         /*
  972.          * realloc the scratch area, since we've run out of room --
  973.          */
  974.             sparsearray = (struct sp_array *) 
  975.                     realloc(sparsearray,
  976.                          2 * sp_array_size * (sizeof(struct sp_array)));
  977.             sp_array_size *= 2;
  978.         }
  979.         if (cc == sizeof buf) {
  980.             if (zero_record(buf)) {
  981.                 if (amidst_data) {
  982.                     sparsearray[sparse_ind++].numbytes
  983.                         = numbytes;
  984.                     amidst_data = 0;
  985.                 }
  986.             } else {  /* !zero_record(buf) */
  987.                     if (amidst_data)
  988.                         numbytes += cc;
  989.                 else {
  990.                         amidst_data = 1;
  991.                     numbytes = cc;
  992.                     sparsearray[sparse_ind].offset
  993.                       = offset;
  994.                 } 
  995.             }
  996.         } else if (cc < sizeof buf) {
  997.           /* This has to be the last bit of the file, so this */
  998.           /* is somewhat shorter than the above. */
  999.                 if (!zero_record(buf)) {
  1000.                 if (!amidst_data) {
  1001.                         amidst_data = 1;
  1002.                     numbytes = cc;
  1003.                     sparsearray[sparse_ind].offset
  1004.                       = offset;
  1005.                 } else
  1006.                     numbytes += cc;
  1007.             } 
  1008.         }
  1009.         offset += cc;
  1010.         clear_buffer(buf);
  1011.     }
  1012.     if (amidst_data)
  1013.             sparsearray[sparse_ind++].numbytes = numbytes;
  1014.     close(fd);
  1015.  
  1016.     return sparse_ind - 1;
  1017. }
  1018.  
  1019. /* 
  1020.  * Just zeroes out the buffer so we don't confuse ourselves with leftover
  1021.  * data.
  1022.  */
  1023. clear_buffer(buf)
  1024.     char    *buf;
  1025. {
  1026.     register int     i;
  1027.  
  1028.     for (i = 0; i < RECORDSIZE; i++)
  1029.         buf[i] = '\0';
  1030. }
  1031.  
  1032. #if 0  /* I'm leaving this as a monument to Joy Kendall, who wrote it */
  1033. /* 
  1034.  * JK - 
  1035.  * This routine takes a character array, and tells where within that array
  1036.  * the data can be found.  It skips over any zeros, and sets the first
  1037.  * non-zero point in the array to be the "start", and continues until it
  1038.  * finds non-data again, which is marked as the "end."  This routine is 
  1039.  * mainly for 1) seeing how far into a file we must lseek to data, given
  1040.  * that we have a sparse file, and 2) determining the "real size" of the
  1041.  * file, i.e., the number of bytes in the sparse file that are data, as
  1042.  * opposed to the zeros we are trying to skip.
  1043.  */
  1044. where_is_data(from, to, buffer)
  1045.     int    *from,
  1046.         *to;
  1047.     char    *buffer;
  1048. {
  1049.     register int    i = 0;
  1050.     register int    save_to = *to;
  1051.     int    amidst_data = 0;
  1052.  
  1053.     
  1054.     while (!buffer[i])
  1055.         i++;
  1056.     *from = i;
  1057.  
  1058.     if (*from < 16)    /* don't bother */
  1059.         *from = 0;
  1060.     /* keep going to make sure there isn't more real
  1061.        data in this record */
  1062.     while (i < RECORDSIZE) {
  1063.         if (!buffer[i]) {
  1064.             if (amidst_data) {
  1065.                 save_to = i;
  1066.                 amidst_data = 0;
  1067.             }
  1068.             i++;
  1069.         }
  1070.         else if (buffer[i]) {
  1071.             if (!amidst_data)
  1072.                 amidst_data = 1;
  1073.             i++;
  1074.         }
  1075.     }
  1076.     if (i == RECORDSIZE)
  1077.         *to = i;
  1078.     else
  1079.         *to = save_to;
  1080.         
  1081. }
  1082. #endif
  1083.  
  1084. /* Note that this routine is only called if zero_record returned true */
  1085. #if 0 /* But we actually don't need it at all. */
  1086. where_is_data (from, to, buffer)
  1087.      int *from, *to;
  1088.      char *buffer;
  1089. {
  1090.   char *fp, *tp;
  1091.  
  1092.   for (fp = buffer; ! *fp; fp++)
  1093.     ;
  1094.   for (tp = buffer + RECORDSIZE - 1; ! *tp; tp--)
  1095.     ;
  1096.   *from = fp - buffer;
  1097.   *to = tp - buffer + 1;
  1098. }
  1099. #endif
  1100.  
  1101.  
  1102.  
  1103. /*
  1104.  * Takes a recordful of data and basically cruises through it to see if
  1105.  * it's made *entirely* of zeros, returning a 0 the instant it finds
  1106.  * something that is a non-zero, i.e., useful data.
  1107.  */
  1108. zero_record(buffer)
  1109.     char    *buffer;
  1110. {
  1111.     register int    i;
  1112.  
  1113.     for (i = 0; i < RECORDSIZE; i++)
  1114.         if (buffer[i] != '\000')
  1115.             return 0;
  1116.     return 1;
  1117. }
  1118.  
  1119. find_new_file_size(filesize, highest_index)
  1120.     int    *filesize;
  1121.     int    highest_index;
  1122. {
  1123.     register int     i;
  1124.  
  1125.     *filesize = 0;
  1126.     for (i = 0; sparsearray[i].numbytes && i <= highest_index; i++)
  1127.         *filesize += sparsearray[i].numbytes;
  1128. }
  1129.     
  1130. /*
  1131.  * Make a header block for the file  name  whose stat info is  st .
  1132.  * Return header pointer for success, NULL if the name is too long.
  1133.  */
  1134. union record *
  1135. start_header(name, st)
  1136.     char    *name;
  1137.     register struct stat *st;
  1138. {
  1139.     register union record *header;
  1140.  
  1141.     header = (union record *) findrec();
  1142.     bzero(header->charptr, sizeof(*header)); /* XXX speed up */
  1143.  
  1144.     /*
  1145.      * Check the file name and put it in the record.
  1146.      */
  1147.     if(!f_absolute_paths) {
  1148.         static int warned_once = 0;
  1149. #ifdef __MSDOS__
  1150.         if(name[1]==':') {
  1151.             name+=2;
  1152.             if(!warned_once++)
  1153.                 msg("Removing drive spec from names in the archive");
  1154.         }
  1155. #endif
  1156. #ifdef amigados
  1157.         if(index (name, ':')) {
  1158.             name = index (name, ':')+1;
  1159.             if(!warned_once++)
  1160.                 msg("Removing volume spec from names in the archive");
  1161.         }
  1162. #endif
  1163.         while ('/' == *name) {
  1164.             name++;                /* Force relative path */
  1165.             if (!warned_once++)
  1166.                 msg("Removing leading / from absolute path names in the archive.");
  1167.         }
  1168.     }
  1169.     strncpy(header->header.name, name, NAMSIZ);
  1170.     if (header->header.name[NAMSIZ-1]) {
  1171.         char *mangled;
  1172.  
  1173.         /* next_mangle(header->header.name); */
  1174.         add_mangle(name,header->header.name);
  1175.         msg("%s: is too long: mangling to %s", name, header->header.name);
  1176.     }
  1177.  
  1178.     to_oct((long) (st->st_mode & ~S_IFMT),
  1179.                     8,  header->header.mode);
  1180. #ifdef amigados
  1181.     if (amiga_perms)
  1182.         to_oct ((long)st->st_amode, 1+12, header->header.devmajor);
  1183. #endif
  1184.     to_oct((long) st->st_uid,    8,  header->header.uid);
  1185.     to_oct((long) st->st_gid,    8,  header->header.gid);
  1186.     to_oct((long) st->st_size,    1+12, header->header.size);
  1187.     to_oct((long) st->st_mtime,    1+12, header->header.mtime);
  1188.     /* header->header.linkflag is left as null */
  1189.     if(f_gnudump) {
  1190.         to_oct((long) st->st_atime, 1+12, header->header.atime);
  1191.         to_oct((long) st->st_ctime, 1+12, header->header.ctime);
  1192.     }
  1193.  
  1194. #ifndef NONAMES
  1195.     /* Fill in new Unix Standard fields if desired. */
  1196.     if (f_standard) {
  1197.         header->header.linkflag = LF_NORMAL;    /* New default */
  1198.         strcpy(header->header.magic, TMAGIC);    /* Mark as Unix Std */
  1199.         finduname(header->header.uname, st->st_uid);
  1200.         findgname(header->header.gname, st->st_gid);
  1201.     }
  1202. #endif
  1203.     return header;
  1204. }
  1205.  
  1206. /* 
  1207.  * Finish off a filled-in header block and write it out.
  1208.  * We also print the file name and/or full info if verbose is on.
  1209.  */
  1210. void
  1211. finish_header(header)
  1212.     register union record *header;
  1213. {
  1214.     register int    i, sum;
  1215.     register char    *p;
  1216.     void bcopy();
  1217.  
  1218.     bcopy(CHKBLANKS, header->header.chksum, sizeof(header->header.chksum));
  1219.  
  1220.     sum = 0;
  1221.     p = header->charptr;
  1222.     for (i = sizeof(*header); --i >= 0; ) {
  1223.         /*
  1224.          * We can't use unsigned char here because of old compilers,
  1225.          * e.g. V7.
  1226.          */
  1227.         sum += 0xFF & *p++;
  1228.     }
  1229.  
  1230.     /*
  1231.      * Fill in the checksum field.  It's formatted differently
  1232.      * from the other fields:  it has [6] digits, a null, then a
  1233.      * space -- rather than digits, a space, then a null.
  1234.      * We use to_oct then write the null in over to_oct's space.
  1235.      * The final space is already there, from checksumming, and
  1236.      * to_oct doesn't modify it.
  1237.      *
  1238.      * This is a fast way to do:
  1239.      * (void) sprintf(header->header.chksum, "%6o", sum);
  1240.      */
  1241.     to_oct((long) sum,    8,  header->header.chksum);
  1242.     header->header.chksum[6] = '\0';    /* Zap the space */
  1243.  
  1244.     userec(header);
  1245.  
  1246.     if (f_verbose) {
  1247.         extern union record *head;        /* Points to current tape header */
  1248.         extern int head_standard;        /* Tape header is in ANSI format */
  1249.  
  1250.         /* These globals are parameters to print_header, sigh */
  1251.         head = header;
  1252.         /* hstat is already set up */
  1253.         head_standard = f_standard;
  1254.         print_header();
  1255.     }
  1256.  
  1257.     return;
  1258. }
  1259.  
  1260.  
  1261. /*
  1262.  * Quick and dirty octal conversion.
  1263.  * Converts long "value" into a "digs"-digit field at "where",
  1264.  * including a trailing space and room for a null.  "digs"==3 means
  1265.  * 1 digit, a space, and room for a null.
  1266.  *
  1267.  * We assume the trailing null is already there and don't fill it in.
  1268.  * This fact is used by start_header and finish_header, so don't change it!
  1269.  *
  1270.  * This should be equivalent to:
  1271.  *    (void) sprintf(where, "%*lo ", digs-2, value);
  1272.  * except that sprintf fills in the trailing null and we don't.
  1273.  */
  1274. void
  1275. to_oct(value, digs, where)
  1276.     register long    value;
  1277.     register int    digs;
  1278.     register char    *where;
  1279. {
  1280.     
  1281.     --digs;                /* Trailing null slot is left alone */
  1282.     where[--digs] = ' ';        /* Put in the space, though */
  1283.  
  1284.     /* Produce the digits -- at least one */
  1285.     do {
  1286.         where[--digs] = '0' + (char)(value & 7); /* one octal digit */
  1287.         value >>= 3;
  1288.     } while (digs > 0 && value != 0);
  1289.  
  1290.     /* Leading spaces, if necessary */
  1291.     while (digs > 0)
  1292.         where[--digs] = ' ';
  1293.  
  1294. }
  1295.  
  1296.  
  1297. /*
  1298.  * Write the EOT record(s).
  1299.  * We actually zero at least one record, through the end of the block.
  1300.  * Old tar writes garbage after two zeroed records -- and PDtar used to.
  1301.  */
  1302. write_eot()
  1303. {
  1304.     union record *p;
  1305.     int bufsize;
  1306.     void bzero();
  1307.  
  1308.     p = findrec();
  1309.     if (p)
  1310.       {
  1311.         bufsize = endofrecs()->charptr - p->charptr;
  1312.         bzero(p->charptr, bufsize);
  1313.         userec(p);
  1314.       }
  1315. }
  1316.